package com.zzx.test.thread;

/**
 * @ClassName: Race
 * @Description:
 * @Author zhouzhixin
 * @Date 2021/5/19
 * @Version 1.0
 */
public class Race implements Runnable {

    @Override
    public void run() {
        for (int i = 1; i <= 100; i++) {
            if ("兔子".equals(Thread.currentThread().getName()) && i == 50) {
                try {
                    Thread.sleep(20);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            if (gameOver(i)) {
                break;
            }

            System.out.println(Thread.currentThread().getName() + ":第" + i + "步");
        }
    }

    private boolean gameOver(int step) {
        if (100 <= step) {
            System.out.println(Thread.currentThread().getName() + "到达终点");
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        Race race = new Race();
        Thread turtleThread = new Thread(race, "乌龟");
        Thread rabbitThread = new Thread(race, "兔子");
        turtleThread.setPriority(Thread.MIN_PRIORITY);
        rabbitThread.setPriority(Thread.MAX_PRIORITY);
        turtleThread.start();
        rabbitThread.start();
    }

}
